package com.hare.common.exception.GlobalException;


import com.hare.common.exception.PasswordNotMatchException;
import com.hare.common.exception.ServiceException;
import com.hare.common.result.R;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * @program: Hare
 * @description: 全局异常处理类
 * @author: Hare
 * @create: 2022-07-01 11:04
 * @Version 6.0.1
 **/
@RestControllerAdvice
@Slf4j
public class GlobalException {

    /**
     * 业务异常
     *
     * @param e       自定义异常类
     * @param request 请求信息
     * @return
     */
    @ExceptionHandler(ServiceException.class)
    public R handleServiceException(ServiceException e, HttpServletRequest request) {
        Integer code = e.getCode();
        log.error("请求地址: {},业务相关异常: {}",request.getRequestURI(),e.getMessage());
        return code != null ? R.error(e.getMessage(), code) : R.error(e.getMessage());
    }

    /**
     * 密码专用处理异常
     *
     * @param e       密码错误异常
     * @param request 请求信息
     * @return
     */
    @ExceptionHandler(PasswordNotMatchException.class)
    public R handleServiceException(PasswordNotMatchException e, HttpServletRequest request) {
        Integer code = e.getCode();
        log.error("请求地址: {},密码异常: {}",request.getRequestURI(),e.getMessage());
        return code != null ? R.error(e.getMessage(), code) : R.error(e.getMessage());
    }
}
